package com.srwl.mytx.contrack;

import android.util.Log;

import com.srwl.mytx.RetrofitManager;
import com.srwl.mytx.domain.Comment;
import com.srwl.mytx.domain.CommonResult;
import com.srwl.mytx.service.CommentService;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class CommentPresenter {
     private static final String TAG = "CommentPresenter";
    private InteractionListener listener;



    /**
     * 提交一个普通评论
     */
    public void commitNormalComment(Comment targetComment ) {
        RetrofitManager.getInstance().createRequest(CommentService.class)
                .createComment(targetComment)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {

                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }

                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }
                        String c_id = result.getData() + "";
                        targetComment.setId(c_id);

                        if (listener != null) {
                            listener.onCreateNormalCommentSuccess(targetComment);
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        Log.e(TAG, "onFailure: " + t.getMessage());
                        if (listener != null) {

                            listener.onCreateCommentError();
                        }
                    }
                });
    }

    /**
     * 创建子评论----就是评论中的回复
     */
    public void commitChildComment(Comment targetComment ) {

        RetrofitManager.getInstance().createRequest(CommentService.class)
                .createChildComment(targetComment)
                .enqueue(new Callback<CommonResult>() {
                    @Override
                    public void onResponse(Call<CommonResult> call, Response<CommonResult> response) {

                        CommonResult result = response.body();
                        if (!response.isSuccessful() || result == null) {
                            Log.e(TAG, "onResponse: " + response.message());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }
                        if (result.isError()) {
                            Log.e(TAG, result.getMsg());
                            if (listener != null) {
                                listener.onCreateCommentError();
                            }
                            return;
                        }
                        if (listener != null) {
                            listener.onCreateChildCommentSuccess(targetComment);
                        }
                    }

                    @Override
                    public void onFailure(Call<CommonResult> call, Throwable t) {
                        Log.e(TAG, "onFailure: " + t.getMessage());
                        if (listener != null) {
                            listener.onCreateCommentError();
                        }

                    }
                });
    }

    public interface InteractionListener{
        void onCreateNormalCommentSuccess(Comment normalComment);
        void onCreateChildCommentSuccess(Comment childComment);
        void onCreateCommentError();
        void showProgressBar(boolean isShowProgressBar);
    }
}
